﻿using DataProvider;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Model;
using Newtonsoft.Json;
using Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Utility;

namespace api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class Sys_DataSourceController : ControllerBase
    {
        public IActionResult GetLeftList(LayPageModel param)
        {
            string condition = "Status=0"; 
            if (!string.IsNullOrEmpty(param.searchKey))
            {
                condition = condition + " and (SourceName like '%"+param.searchKey+"%') ";
            }
                var List = Sys_DataSourceService.Instances.QueryCustom(condition).OrderBy(x => x.OrderNum).ToList();
            
            return Ok(JsonConvert.SerializeObject(new ResultData<object>() { code = 0, data = List,count=List.Count }));
        }
        public IActionResult GetRightList(LayPageModel param)
        {
            string condition = "Status=0 and SourceId=" + param.OtherId;
            if (!string.IsNullOrEmpty(param.searchKey))
            {
                condition = condition + " and (SourceName like '%" + param.searchKey + "%') ";
            }
            var List = Sys_DataSourceItemService.Instances.QueryCustom(condition).OrderBy(x=>x.OrderNum).ToList();
            return Ok(JsonConvert.SerializeObject(new ResultData<object>() { code = 0, data = List, count = List.Count }));
        }
        public IActionResult SaveDataSource(CommonSaveModel param)
        {
            var userToken = (UserCache)JwtHelp.GetToken(HttpContext.Request.Headers["Authorization"].ToString()).data; ;
            var Ent = JsonConvert.DeserializeObject<Sys_DataSource>(param.strEntity);
            if (Ent.Id == 0)
            {
                Ent.CreateUser = userToken.UserId;
                Ent.CreateTime = DateTime.Now;
                Sys_DataSourceService.Instances.Add(userToken.UserId, Ent);
            }
            else
            {
                Ent.LastUpdateTime = DateTime.Now;
                Ent.LastUpdateUser = userToken.UserId;
                Sys_DataSourceService.Instances.Edit(userToken.UserId, Ent);
            }
            return Ok(JsonConvert.SerializeObject(new ResultData<object>() { code = 0, data = "Success" }));
        }
        public IActionResult SaveDataSourceItem(CommonSaveModel param)
        {
            var userToken = (UserCache)JwtHelp.GetToken(HttpContext.Request.Headers["Authorization"].ToString()).data; ;
            var Ent = JsonConvert.DeserializeObject<Sys_DataSourceItem>(param.strEntity);
            if (Ent.Id == 0)
            {
                Ent.CreateUser = userToken.UserId;
                Ent.CreateTime = DateTime.Now;
                Sys_DataSourceItemService.Instances.Add(userToken.UserId, Ent);
            }
            else
            {
                Ent.LastUpdateTime = DateTime.Now;
                Ent.LastUpdateUser = userToken.UserId;
                Sys_DataSourceItemService.Instances.Edit(userToken.UserId, Ent);
            }
            return Ok(JsonConvert.SerializeObject(new ResultData<object>() { code = 0, data = "Success" }));
        }
        public IActionResult DelSourceItem(CommonIds param)
        {
            var userToken = (UserCache)JwtHelp.GetToken(HttpContext.Request.Headers["Authorization"].ToString()).data; ;
            var List = Sys_DataSourceItemService.Instances.GetListByIds(param.Ids);
            foreach (var d in List)
            {
                d.LastUpdateTime = DateTime.Now;
                d.LastUpdateUser = userToken.UserId;
                d.Status = Status.delete;

            }
            Sys_DataSourceItemService.Instances.EditBatch(userToken.UserId, List);
            return Ok(JsonConvert.SerializeObject(new ResultData<object>() { code = 0, data = "Delete Success" }));
        }
        public IActionResult DelSource(CommonIds param)
        {
            var userToken = (UserCache)JwtHelp.GetToken(HttpContext.Request.Headers["Authorization"].ToString()).data; ;
            var List = Sys_DataSourceService.Instances.GetListByIds(param.Ids);
            foreach (var d in List)
            {
                d.LastUpdateTime = DateTime.Now;
                d.LastUpdateUser = userToken.UserId;
                d.Status = Status.delete;
            }
            Sys_DataSourceService.Instances.EditBatch(userToken.UserId, List);
            return Ok(JsonConvert.SerializeObject(new ResultData<object>() { code = 0, data = "Delete Success" }));
        }
    }
}
